# -*- coding: utf-8 -*-
__author__ = 'YinDu'

import pandas as pd
from utils.PathTool import *
from utils.RequestTool import *
from utils.ProjectTool import *


class StockExecelView(Resource):
    """
        Execel表数据
    """

    def __init__(self, path_execl='stock.xlsx'):
        self.wb = pd.read_excel(to_path(path_execl))
        self.data = self.wb.values

    def get(self):
        """
            趋势图
        """
        # 所有数据,所有时间,09:30:00-09:35:00数据,09:30:00-09:35:00时间,09:35:00-09:40:00数据，09:35:00-09:40:00时间
        data_l, time_l, data_lt1, time_lt1, data_lt2, time_lt2 = [], [], [], [], [], []
        for the_data in self.data:
            date_now_str = str_change_timestamp(datetime_now() + ' ' + the_data[0])
            if date_now_str >= str_change_timestamp(
                    datetime_now() + ' ' + '09:30:00') and date_now_str < str_change_timestamp(
                datetime_now() + ' ' + '09:35:00'):
                data_lt1.append(the_data[1])
                time_lt1.append(the_data[0])
            if date_now_str >= str_change_timestamp(
                    datetime_now() + ' ' + '09:35:00') and date_now_str < str_change_timestamp(
                datetime_now() + ' ' + '09:40:00'):
                data_lt2.append(the_data[1])
                time_lt2.append(the_data[0])
            data_l.append(the_data[1])
            time_l.append(the_data[0])
        maxs_data, mins_data = stockt.data_web_max(data_l), stockt.data_web_min(data_l)
        maxs_data1, mins_data1 = stockt.data_web_max(data_lt1), stockt.data_web_min(data_lt1)
        maxs_data2, mins_data2 = stockt.data_web_max(data_lt2), stockt.data_web_min(data_lt2)
        td_data = [[data_lt1[0], data_lt1[-1], min(data_lt1), max(data_lt1)],
                   [data_lt2[0], data_lt2[-1], min(data_lt2), max(data_lt2)]]
        return normal_response(
            data={'data_l': data_l, 'time_l': time_l, 'data_lt1': data_lt1, 'time_lt1': time_lt1, 'data_lt2': data_lt2,
                  'time_lt2': time_lt2, 'maxs_data': maxs_data, 'mins_data': mins_data, 'maxs_data1': maxs_data1,
                  'mins_data1': mins_data1, 'maxs_data2': maxs_data2, 'mins_data2': mins_data2, 'td_data': td_data})

    def post(self):
        """
            蜡烛图
        """
        td_data = []
        list_time = []
        change_data = []
        init_date = 0
        for the_data in self.data:
            htime, mtime, stime = the_data[0].split(':')
            star_time = int(mtime) // 5 * 5
            if init_date != star_time:
                if change_data != []:
                    td_data.append([change_data[0], change_data[-1], min(change_data), max(change_data)])
                    list_time.append('{}:{}:00'.format(htime, mtime))
                    change_data = []
                init_date = star_time
            change_data.append(the_data[1])

        return normal_response(data={'list_time': list_time, 'td_data': td_data})


class StockLookView(Resource):
    def get(self):
        """
            实时数据
        """
        data_l = []
        time_l = []
        stock_name = 'sh000006'
        for the_key, the_value in wot.with_open_text_read(
                to_path('utils\\textbool\\' + str(stock_name) + '_' + str(datetime_now()) + '.txt'), 'r').items():
            data_l.append(the_value)
            time_l.append(int_change_str_time(the_key))
        return normal_response(data={'data_l': data_l, 'time_l': time_l})
